package com.ddd.test.service;

import com.ddd.test.aggregate.aggrorder.OrderEntity;
import com.ddd.test.aggregate.aggrsales.SalesEntity;
import com.ddd.test.domainevent.ChangeLevel;
import com.ddd.test.domainserv.NewStaffTakesOrders;
import com.ddd.test.dto.cmd.OrderDTO;
import com.ddd.test.dto.cmd.SalesDTO;
import com.ddd.test.repo.SalesRepo;
import com.ddd.test.result.Result;
import com.ddd.test.service.SalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * SalesService服务实现类
 *
 * 
 */
@Service
public class SalesServiceImpl implements SalesService{

  @Autowired
  SalesRepo salesRepo;
 
  @Autowired
  NewStaffTakesOrders newStaffTakesOrders;
  
  @Autowired
  ChangeLevel changeLevel;
  /**
   * 按code禁用
   *
   */
  public Result<String> disable(String code) {
    Result<String> result = new Result<String>(true);
    if (code == null) {
      throw new IllegalStateException("code不能为空");
    }
    SalesEntity salesEntity = salesRepo.getSalesEntity(code);
    if (salesEntity == null) {
        throw new IllegalStateException("没有找到聚合code");
      }
    salesRepo.remove(salesEntity);
    result.setObj("success");
    return result;
  }

  /**
   * 创建用户
   *
   */
 
  public Result<String> create(SalesDTO record) {

    Result<String> result = new Result<String>(true);
    if (record == null) {
    	throw new IllegalStateException("参数record不能为空");
    }
    if (record.getCode() == null) {
    	throw new IllegalStateException("code属性不能为空");
    }
    SalesEntity salesEntity = salesRepo.getSalesEntity(record.getCode());
    if (salesEntity != null) {
        throw new IllegalStateException("聚合code已存在");
    }
    salesEntity =  SalesEntity.createSalesEntity(record.getCode(), record.getName(), record.getDescription(), 
			  record.getLevel(), record.getSalesAreaList());
    salesRepo.add(salesEntity);
    result.setObj("success");
    return result;
  }

  /**
   * 按code查询
   *
   */
  public Result<SalesDTO> getSales(String code){
    Result<SalesDTO> result = new Result<SalesDTO>(true);
    if (code == null) {
      throw new IllegalStateException("code不能为空");
    }
    SalesEntity salesEntity = salesRepo.getSalesEntity(code);
    SalesDTO salesDTO = convert2Sales(salesEntity);
    result.setObj(salesDTO);
    return result;
  }

  //TODO
  private SalesDTO convert2Sales(SalesEntity salesEntity) {
	  return new SalesDTO();
  }
  /**
   * 调级
   *
   * @return Result<String>
   */
  public Result<Integer> changeLevel(Integer level,String code) {
    Result<Integer> result = new Result<Integer>(true);
    SalesEntity salesEntity = salesRepo.getSalesEntity(code);
    if (salesEntity == null) {
      throw new IllegalStateException("没找到实体");
    }
    salesEntity.changeLevel(level);
    salesRepo.add(salesEntity);
    //发布调级领域事件
    changeLevel.notice(salesEntity);
    result.setObj(level);
    return result;
  }
  
  /**
   * 新人带客户资源入职
   *
   * @return Result<String>
   */
  public Result<String> entryAndOrders(SalesDTO salesDTO,OrderDTO orderDTO) {
    Result<String> result = new Result<String>(true);
    SalesEntity salesEntity =  SalesEntity.createSalesEntity(salesDTO.getCode(), salesDTO.getName(), salesDTO.getDescription(), 
    		salesDTO.getLevel(), salesDTO.getSalesAreaList());
    OrderEntity orderEntity = OrderEntity.createOrderEntity(orderDTO.getOrderNo(),
			  orderDTO.getProduct(), orderDTO.getCount(), orderDTO.getPrice(), orderDTO.getSalesCode());
    newStaffTakesOrders.entryTakeOrders(salesEntity,orderEntity);
    result.setObj("success");
    return result;
  }

}